<?php
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'DAOEventSuscription.php');
require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');
require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'tables'.DS.'suscripcionEvento.php');

class JDAOEventSuscription implements DAOEventSuscription{
	/* (non-PHPdoc)
	 * @see DAOEventSuscription::suscribeEvent()
	 */
	public function suscribeEvent($dtoEventSuscription) {
		$factoria=DTOFactory::getInstance();
		$row=$factoria->fromDTOEventSuscription2EventTable($dtoEventSuscription);
		if(!$row->store()){
	        JError::raiseError(500, $row->getError() );
	    }	
	    return $row->idSuscripcionEvento;	
	}

	/* (non-PHPdoc)
	 * @see DAOEventSuscription::checkEventSuscription()
	 */
	public function checkEventSuscription($eventID, $userID) {
		$suscritedToEvent = $this -> singleObjectQuery("SELECT idSuscripcionEvento FROM #__suscripcionevento
														WHERE idEvento= ".$eventID." AND idUsuario= ".$userID);
		if ($suscritedToEvent!=null)
			return true;
		else
			return false;
	}
	
	public function readUserSuscribeEvents($idUser)
	{
		// sacamos la lista de eventos suscritos por un usuario 
		//$listaEventosSuscritos = $this->multipleObjectQuery("SELECT * FROM #__evento
			//												 INNER JOIN #__suscripcionevento ON #__evento.idEvento = #__suscripcionevento.idEvento WHERE #__suscripcionevento.idUsuario = ".$idUser);
		$listaEventosSuscritos=$row=$this->multipleRowQuery("select e.idEvento,e.idCreador,e.idCategoria,e.idAreaAdministrativa, e.estado, e.nombreEvento, e.descripcionEvento, e.imagenEvento, e.coordenada, e.fecha, e.numvisitas, e.hora, u.name as nameCreator, c.nombreCategoria , a.nombreAreaAdministrativa from #__evento as e inner join #__users as u  ON e.idCreador=u.id inner join #__categoria as c ON e.idCategoria=c.idCategoria inner join #__areaadministrativa as a ON e.idAreaAdministrativa=a.idAreaAdministrativa INNER JOIN #__suscripcionevento as se ON e.idEvento = se.idEvento WHERE se.idUsuario = ".$idUser);
		// la factoria la convierte en una lista de DtoEventSuscription
		$factoria=DTOFactory::getInstance();
		$listaDtoEventSuscription=$factoria->fromEventArrayList2DTOEventList($listaEventosSuscritos);
		//$listaDtoEventSuscription=$factoria->fromObjectList2DTOEventList($listaEventosSuscritos);
		return $listaDtoEventSuscription;
	}
	public function deleteEventSubscription($idEvent,$idUser){
		$factoria=DTOFactory::getInstance();
		$subscripcionEvento=$this->singleObjectQuery("SELECT * FROM #__suscripcionevento WHERE idUsuario = ".$idUser." AND idEvento = ".$idEvent);
		$row=$factoria->fromEventSubscriptionObject2Table($subscripcionEvento);
		$row->delete($subscripcionEvento->idSuscripcionEvento);
	}
	
	public function deleteEventSubscriptions($idEvent){
		//hay que poner otro metodo, por que el multiple query no sirve
		$db=& JFactory::getDBO();
		$query='DELETE FROM #__suscripcionevento WHERE idEvento = '.$idEvent;
		$db->setQuery($query);
		$db->query();
		
		/*
		$factoria=DTOFactory::getInstance();
		$suscripsionsEvent=$this->multipleObjectQuery("DELETE FROM #__suscripcionevento WHERE idEvento = ".$idEvent);*/
	}
	
	private function singleObjectQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		$row = $db->loadObject();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		return $row;
	}
	private function multipleObjectQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery($query);
		return $db->loadObjectList();
	}
	private function multipleRowQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery($query);
		return $db->loadAssocList();
	}

	
}

?>